//var socket =io('http://127.0.0.1:3001')
var a = 1
var username, avatar, money
var socket = io('192.168.1.119:3000')
$('#login_box li').on('click', function() {
    $(this).addClass('now').siblings().removeClass('now')
})
//------眼睛显示钱-----
$('.own .fa-eye').on('click', function() {
    $(this).toggleClass('fa-eye-slash')
    if ($('.own .showmoney').css('display') == 'none') {
        $('.own .showmoney').css('display', 'inline')
        $('.own .hidemoney').css('display', 'none')
    } else {
        $('.own .showmoney').css('display', 'none')
        $('.own .hidemoney').css('display', 'inline')
    }
})
//------眼睛显示钱
//登录逻辑
$('#login_btn').on('click', function() {
    username = $('#user_name').val().trim()
    if (!username) {
        alert('请输入用户名')
        return
    }
    avatar = $('#login_avatar li.now img').attr('src')
    console.log(avatar)
    socket.emit('login', {
        username,
        avatar
    })
})
socket.on('loginErr', data => {
    alert('登录失败')
})
socket.on('loginSuccess', data => {
    alert('登录成功')
    $('.login_box').fadeOut()
    $('#contaniner').fadeIn()
    //console.log(data)
    //设置个人信息
    $('.own img').attr('src', data.avatar)
    $('.own .user_name').text(data.username)
    $('.own .user_money .showmoney').text(data.money.toFixed(2))
    username = data.username
    avatar = data.avatar
    money = data.money
})
//监听添加用户消息
socket.on('addUser', data => {
    $('#msgs').append(`
	<div class="msg min time" id="histStart">${data.username}进入了群聊</div>
		`)
    scrollIntoView()
})
//更新好友列表
socket.on('userList', data => {
    $('#hots').html('')
    data.forEach(item => {
        $('#hots').append(`
			<li class="rel-item"><img src="${item.avatar}" alt="">
			<span>${item.username}</span></li>
			`)
    })
    $('#userCount').text(data.length)
    //console.log(data)
})
socket.on('delUser', data => {
    $('#msgs').append(`
	<div class="msg min time" id="histStart">${data.username}离开了群聊</div>
		`)
    scrollIntoView()
})
$('#submit').on('click', () => {
    let content = $('#editor').html()
    $('#editor').html('')
    if (!content) return alert('请输入后发送')
    socket.emit('sendMessage', {
        msg: content,
        username,
        avatar
    })
})
socket.on('receiveMessage', data => {
    if (data.username === username) {
        //自己的消息
        $('#msgs').append(`
			<div class="msg guest">
                <div class="msg-right">
                     <div class="msg-host photo" style="background-image: url(${data.avatar})"></div>
                    <div class="msg-ball" title="今天 17:52:06">${data.msg}</div>
                </div>
            </div>
			`)
    } else {
        //别人的消息
        $('#msgs').append(`
			<div class="msg robot">
                <div class="msg-left" worker="${data.username}">
                    <div class="msg-host photo" style="background-image: url(${data.avatar})"></div>
                    <div class="msg-ball" title="今天 17:52:06">${data.msg}</div>
                </div>
            </div>
		`)
    }
    scrollIntoView()
})

function scrollIntoView() {
    $('#msgs').children(':last').get(0).scrollIntoView(false)
}
$('#file').on('change', function() {
    console.log(this)
    var file = this.files[0]
    console.log(file)
    var fr = new FileReader()
    fr.readAsDataURL(file)
    fr.onload = function() {
        console.log()
        socket.emit('sendImage', {
            username,
            avatar,
            img: fr.result
        })
    }
})
socket.on('receiveImage', data => {
    if (data.username === username) {
        //自己的消息
        $('#msgs').append(`
			<div class="msg guest">
                <div class="msg-right">
                     <div class="msg-host photo" style="background-image: url(${data.avatar})"></div>
                    <div class="msg-ball" title="今天 ${new Date().toLocaleTimeString()}"><img src="${data.img}" alt=""></div>
                </div>
            </div>
			`)
    } else {
        //别人的消息
        $('#msgs').append(`
			<div class="msg robot">
                <div class="msg-left" worker="${data.username}">
                    <div class="msg-host photo" style="background-image: url(${data.avatar})"></div>
                    <div class="msg-ball" title="今天${new Date().toLocaleTimeString()}"><img src="${data.img}" alt=""></div>
                </div>
            </div>
		`)
    }
    $('#msgs img:last').on('load', function() {
        scrollIntoView()
    })
})
$('.face').on('click', function() {
    console.log(this)
    $("#editor").emoji({
        button: '.face',
        showTab: false,
        animation: 'slide',
        position: 'topRight',
        icons: [{
            name: "QQ表情",
            path: "/dist/img/qq/",
            maxNum: 91,
            excludeNums: [41, 45, 54],
            file: ".gif"
        }]
    });
})
//初始化emoji
// //--------------------------------查看红包逻辑开始--------------------------------
// $('#wallet').on('click', function(ev) {
//     console.log('执行了2次') 
//      ev.stopPropagation();
//     $('.parent').fadeIn(200) 
//     $(document).on('click', function(ev) {
//         $('.parent').fadeOut(200);
        
//     })
//     $('.parent').on('click', function(ev) {
//         ev.stopPropagation()
       
//     })
//     $('.send').hover(function() {
//         $(this).css('background-color', '#ee8877')
//     }, function(){
//         $(this).css('background-color', '#FFB6C1')
       
//     })
    
       

// })
//--------------------------------查看红包逻辑开始--------------------------------
$('#wallet').on('click', function(ev) {
     ev.stopPropagation();
    $('.parent').fadeIn(200) 
})
$(document).on('click', function(ev) {
    num.val('')
    sum.val('')
    $('.parent').fadeOut(200);
})
$('.parent').on('click', function(ev) {
    ev.stopPropagation()       
})
$('.send').hover(function() {
    $(this).css('background-color', '#ee8877')
}, function(){
    $(this).css('background-color', '#FFB6C1')
})
function getRandom(two, one){
    return Math.random() * (two - one) + one;
}
var num = $('#numberbox .number');
var sum = $('#moneybox .money');
var word =$('.Copywriting')
var min = 0.01;
var arr,oSum,_oSum
$('.send').on('click',function(ev){
    arr=[];
    oSum = sum.val().trim();
    oNum = num.val().trim();
    _oSum=oSum
    num.val('')
    sum.val('')
    for (var i = 1; i <= oNum; i++) {
         if (i == oNum) {
            arr.push(oSum)
         }else {
            var safe_total = (oSum - (oNum - i) * min) / (oNum - i);
            var money = (getRandom(+min, +safe_total)).toFixed(2);
            arr.push(money)
            oSum = (oSum - money).toFixed(2);
         }
    }
    $('.parent').fadeOut(100)
    socket.emit('sendmoney',{arr:arr,username:username,avatar:avatar,dd:_oSum})
        
})

socket.on('changemoney',function(data){
    //更新金额   

    console.log(data)    
    if (data.username === username) {
        //自己的消息   
        $('#msgs').append(`
            <div class="msg-right">
                                <div class="msg-host photo" style="background-image: url(${data.avatar})"></div>
                                <div class="wrap_box ">
                                    <a href="javascript:;">
                                        <div class="top1">
                                            <img src="./imgs/pack.jpg">
                                            <div class="box">
                                                <span>恭喜发财,大吉大利!</span><br>
                                                <span>领取红包</span>
                                            </div>
                                        </div>
                                    </a>
                                    <div class="buttom">
                                        <div class="box1">
                                            <span>微信红包</span><img src="./imgs/we.png" alt="">
                                        </div>
                                </div>
                    </div>
                </div>
            `)
    } else {
        //别人的消息
        $('#msgs').append(`
            <div class="msg-left" worker="小龙">
                                <div class="msg-host photo" style="background-image: url(${data.avatar})"></div>
                                <div class="wrap_box ">
                                    <a href="javascript:;">
                                        <div class="top1">
                                            <img src="./imgs/pack.jpg">
                                            <div class="box">
                                                <span>恭喜发财,大吉大利!</span><br>
                                                <span>领取红包</span>
                                            </div>
                                        </div>
                                    </a>
                                    <div class="buttom">
                                        <div class="box1">
                                            <span>微信红包</span><img src="./imgs/we.png" alt="">
                                        </div>
                                    </div>
                                </div>
             </div>
        `)
    }
   scrollIntoView() 
})
 socket.on('upmoney',data=>{
        console.log(data)
     $('.own .user_money .showmoney').text(data)
})
// $('.send').on('click',function(ev){
//         ev.stopPropagation()
//         var num = $('#numberbox .number');
//         var sum = $('#moneybox .money');
//         var word =$('.Copywriting')
//         var min = 0.01;
//         var arr = []
//         var oSum = sum.val().trim();
//         var _oSum=oSum
//         var oNum = num.val().trim();
//             function getRandom(two, one){
//              return Math.random() * (two - one) + one;
//             }               
//             num.val('')
//             sum.val('') 
//             for (i = 1; i <= oNum; i++) {
//                 if (i == oNum) {
//                     arr.push(oSum)
//                 }else {
//                     var safe_total = (oSum - (oNum - i) * min) / (oNum - i);
//                     var money = (getRandom(+min, +safe_total)).toFixed(2);
//                     arr.push(money)
//                     oSum = (oSum - money).toFixed(2);
//                 }
//             }     
//         socket.emit('sendmoney',{arr:arr,username:username,avatar:avatar,dd:_oSum})
//         console.log('执行了1次')                
//     })
//-------------------------------红包逻辑结束--------------------------------



//-------------------------------发红包逻辑开始------------------------------  
    
      
    //    console.log(arr)
    // }
    // function getRandom(two, one) {
    //     return Math.random() * (two - one) + one;
    // }
     


//-------------------------------发红包逻辑开始------------------------------